package nl.hva.three2learn.controller;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import nl.hva.three2learn.authentication.Role;
import nl.hva.three2learn.authentication.UserAuthentication;
import nl.hva.three2learn.dao.CategoryDao;
import nl.hva.three2learn.exception.ResourceNotFoundException;
import nl.hva.three2learn.model.Category;
import nl.hva.three2learn.model.Course;
import nl.hva.three2learn.util.Validate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/category")
public final class CategoryController extends BaseController {

	@Autowired
	private CategoryDao categoryDao;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping(value = "/overview_manage/{categoryId}", method = RequestMethod.GET)
	public String startManageCategory(UserAuthentication userAuthenticate,
			HttpSession session,
			@PathVariable("categoryId") long categoryId) {
		
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		session.setAttribute("category", categoryDao.findById(categoryId));
		return "category/manage_category";
	}
	
	@RequestMapping(value = "/start_add_category", method = RequestMethod.GET)
	public String startAddCategory(UserAuthentication userAuthenticate) {
		
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		return "category/add_category";
	}
	
	@RequestMapping(value = "/add_category", method = RequestMethod.POST)
	public String addCategory(UserAuthentication userAuthenticate,
			HttpSession session,
			Locale locale,
			
			@RequestParam("categoryName") String categoryName)
	{
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		categoryDao.save(new Category(categoryName));
		flash(session, messageSource.getMessage("category.new", null, locale));
		return "redirect:/admin/";
	}
	
	@RequestMapping(value = "/manage_category", method = RequestMethod.POST)
	public String manageCategory(UserAuthentication userAuthenticate,
			
			@RequestParam("categoryId") long categoryId,
			HttpSession session,
			Locale locale,
			@RequestParam("categoryName") String categoryName) {
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		Category category = categoryDao.findById(categoryId);
		category.setName(categoryName);
		
		categoryDao.save(category);
		flash(session, messageSource.getMessage("category.edit", null, locale));
		return "redirect:/admin/";
	}
	
	@RequestMapping("/overview_manage")
	public String overviewManage(UserAuthentication userAuthenticate,
			Model model) {
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		List<Category> categories = categoryDao.findAllCategories();
		
		model.addAttribute("categories", categories);
		
		return "category/overview_manage";
	}
	
	@RequestMapping("/overview")
	public String overview(Model model) {
		
		List<Category> categories = categoryDao.findAllCategories();
		
		model.addAttribute("categories", categories);
		
		return "category/overview";
	}
	
	@RequestMapping(value = "/overview/{categoryId}", method = RequestMethod.GET)
	public ModelAndView courses(@PathVariable("categoryId") long categoryId) {
		
		Category category = categoryDao.findById(categoryId);
		
		if (category == null)
			throw new ResourceNotFoundException();
		
		List<Course> tmp = category.getCourses();
		
		List<Course> courses = new LinkedList<Course>(); 
		
		for (Course c : tmp) {
			if (!c.isDisabled()) {
				courses.add(c);
			}
		}
		
		ModelAndView modelAndView = new ModelAndView("category/courses");
		modelAndView.addObject("category", category);
		modelAndView.addObject("courses", courses);
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public String removeCategory(UserAuthentication userAuthenticate,
			@RequestParam("categoryId") long categoryId) {
		
		if(!userAuthenticate.isInRole(Role.ADMIN) && !userAuthenticate.isInRole(Role.TEACHER)) {
			return "redirect:/";
		}
		
		Category category = categoryDao.findById(categoryId);
		
		if(category == null) {
			return "redirect:/";
		}
		categoryDao.delete(category);
		
		return "redirect:/admin/";
	}
}
